package com.trace.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trace.app.entity.DTO.RawProDTO;
import com.trace.app.entity.Raw;
import com.trace.app.entity.RawPro;
import com.trace.app.entity.ResponseResult;
import com.trace.app.entity.consts.AppHttpCodeEnum;
import com.trace.app.entity.vo.LabelValueVo;
import com.trace.app.entity.vo.PageVo;
import com.trace.app.entity.vo.RawVo;
import com.trace.app.mapper.RawProMapper;
import com.trace.app.service.RawProService;
import com.trace.app.service.RawService;
import com.trace.app.util.BeanCopyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (RawPro)表服务实现类
 *
 * @author lionkliu
 * @since 2024-04-21 13:27:35
 */
@Service("rawProService")
public class RawProServiceImpl extends ServiceImpl<RawProMapper, RawPro> implements RawProService {
    @Resource
    private RawService rawService;

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<RawPro> queryWrapper = new LambdaQueryWrapper<>();
        Page<RawPro> page = page(new Page<>(pageNum, pageSize), queryWrapper);
        PageVo<RawPro> pageVo = new PageVo<>();
        pageVo.setResult(page.getRecords());
        pageVo.setTotal(page.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(RawPro rawPro) {
        return save(rawPro) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(RawPro rawPro) {
        LambdaUpdateWrapper<RawPro> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(RawPro::getId, rawPro.getId());
        // TODO
        update(null, updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getRawByProId(Integer proId) {
        LambdaQueryWrapper<RawPro> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RawPro::getProductId, proId);
        List<RawPro> rawProList = list(queryWrapper);
        List<RawVo> collect = rawProList.stream().map(rawPro -> {
            RawVo rawVo = BeanCopyUtils.copyBean(rawPro, RawVo.class);
            Raw raw = rawService.getById(rawPro.getRawId());
            rawVo.setName(raw.getName());

            return rawVo;
        }).collect(Collectors.toList());

        return ResponseResult.okResult(collect);
    }

    @Override
    public ResponseResult getRawLabelValueByProId(Long proId) {
        LambdaQueryWrapper<RawPro> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RawPro::getProductId, proId);

        List<LabelValueVo> collect = list(queryWrapper).stream().map((rawPro -> {
            LabelValueVo labelValueVo = new LabelValueVo();
            labelValueVo.setValue(String.valueOf(rawPro.getRawId()));
            LambdaQueryWrapper<Raw> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Raw::getId, rawPro.getRawId());
            Raw raw = rawService.getOne(wrapper);

            labelValueVo.setLabel(raw.getName());
            return labelValueVo;
        })).collect(Collectors.toList());
        return ResponseResult.okResult(collect);
    }

    @Override
    public ResponseResult insertBatch(RawProDTO rawProDTO) {
        Integer[] rawIds = rawProDTO.getRawIds();
        Arrays.stream(rawIds).forEach(rawId -> {
            LambdaQueryWrapper<RawPro> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RawPro::getProductId, rawProDTO.getProId())
                    .eq(RawPro::getRawId, rawId);
            RawPro one = getOne(queryWrapper);
            if (Objects.isNull(one)) {
                RawPro rawPro = new RawPro();
                rawPro.setRawId(rawId)
                        .setProductId(rawProDTO.getProId());
                save(rawPro);
            }
        });

        return ResponseResult.okResult();
    }
}

